﻿using System.Collections.Generic;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Templates.Models;

namespace Templates.UnitTests
{
    [TestClass]
    public class SerializerTest
    {
        private Serializer _serializer = new Serializer();

        /// <summary>
        ///A test for Open
        ///</summary>
        [TestMethod()]
        public void OpenTest()
        {
            Template actual = _serializer.Open(TestConstants.TestTemplate);

            Assert.AreEqual("[", actual.StartCharacter);
            Assert.AreEqual("]", actual.EndCharacter);
            Assert.AreEqual(true, actual.IgnoreCase);
            Assert.AreEqual(1, actual.Variables.Count);
            Assert.AreEqual("WOOT", actual.Variables[0].Name);
            Assert.AreEqual("Hello!", actual.Variables[0].Value);
        }

        /// <summary>
        ///A test for Save
        ///</summary>
        [TestMethod()]
        public void SaveTest()
        {
            string path = Path.GetTempFileName();
            try
            {
                Template template = new Template
                {
                    StartCharacter = "[",
                    EndCharacter = "]",
                    IgnoreCase = true,
                    Variables = new List<Variable>
                    {
                        new Variable { Name = "WOOT", Value = "Hello!" },
                    },
                };
                _serializer.Save(path, template);
                Assert.IsTrue(File.Exists(path));
                Assert.IsTrue(new FileInfo(path).Length > 0);
            }
            finally
            {
                File.Delete(path);
            }
        }
    }
}
